﻿using System;
using System.Configuration;
using System.Diagnostics;

namespace Microsoft.Coding4Fun.TweetCraft.Tray
{
    internal static class TweetCraftExtensions
    {
        public static void ApplySettingsProperty(this ITweetCraft tweetCraft, string propertyName)
        {
            if (tweetCraft == null)
                throw new ArgumentNullException("tweetCraft");

            switch (propertyName)
            {
                case "TwitterToken":
                    tweetCraft.TwitterToken = Settings.Default.TwitterToken;
                    break;
                case "TwitterTokenSecret":
                    tweetCraft.TwitterTokenSecret = Settings.Default.TwitterTokenSecret.ToSecureString();
                    break;
                case "WowAccount":
                    if (!String.IsNullOrEmpty(Settings.Default.WowAccount))
                        tweetCraft.WowAccount = Settings.Default.WowAccount;
                    break;
                case "AutoTweetScreenshots":
                    tweetCraft.AutoTweetScreenshots = Settings.Default.AutoTweetScreenshots;
                    break;
            }

            Trace.TraceInformation("TweetCraft setting '{0}' applied.", propertyName);
        }

        public static void ApplySettings(this ITweetCraft tweetCraft)
        {
            if (tweetCraft == null)
                throw new ArgumentNullException("tweetCraft");

            using (TraceScope trace = TraceScope.Information("Applying TweetCraft settings..."))
            {
                foreach (SettingsProperty property in Settings.Default.Properties)
                    tweetCraft.ApplySettingsProperty(property.Name);

                trace.Complete("TweetCraft settings applied.");
            }
        }
    }
}
